package xyz.xpman.prisonerfitness.preference;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceViewHolder;

import org.jetbrains.annotations.NotNull;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.util.StrKit;

/**
 * 自定义PreferenceCategory类，设置PreferenceCategory的title的字体大小、颜色，控制大小写等。
 */
@SuppressWarnings("unused")
@RequiresApi(api = Build.VERSION_CODES.M)
public class MyPreferenceCategory extends PreferenceCategory {

    public MyPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        LinearLayout linearLayout = (LinearLayout) holder.itemView;

        holder.setDividerAllowedAbove(false);
        holder.setDividerAllowedBelow(false);

        logChild(linearLayout);
    }


    private void logChild(@NotNull ViewGroup layout) {
        logChild(layout, true);
    }

    @SuppressLint({"ResourceAsColor", "SetTextI18n", "UseCompatLoadingForDrawables"})
    protected void logChild(@NotNull ViewGroup layout, boolean recursion) {

        for (int i = 0; i < layout.getChildCount(); i++) {
            View view = layout.getChildAt(i);
            Log.i("[onBindViewHolder]", "view: " + view.getClass());


            if (view instanceof ViewGroup) {
                if (recursion) {
                    logChild((ViewGroup) view, true);
                }
            } else if (view instanceof TextView) {
                CharSequence value = ((TextView) view).getText();
                Log.i("[onBindViewHolder]", "value: " + value);

                if (StrKit.notBlank(String.valueOf(value))) {
                    processStyle(((TextView) view));
                }
            } else {
                return;
            }
        }
    }


    @SuppressLint({"RtlHardcoded", "ResourceAsColor"})
    protected void processStyle(@NotNull TextView view) {
        view.setTextSize(16);
        // 设置title文本不全为大写
        view.setAllCaps(false);

//        view.setTextColor(R.color.orange);
        view.setTextColor(getContext().getColor(R.color.orange));
//        view.setTextColor(Color.parseColor("#FFFD5722"));

    }

}
